DSC 40B

Problems tagged with "recurrence relations"

Problem #107

Tags: lecture-05, recurrence relations

State (but do not solve) the recurrence describing the run time of the following code, assuming that the input, arr, is a list of size \(n\).

def foo(arr, stop=None):
    if stop is None:
        stop = len(arr) - 1

    if stop < 0:
        return 1

    last = arr[stop]
    return last * foo(arr, stop - 1)
Solution

\(T(n) = T(n-1) + \Theta(1)\).

Each call does a constant amount of work (a comparison, an index and a multiplication) and makes one recursive call with stop one smaller, so on a problem one element smaller.

Problem #109

Tags: lecture-05, recurrence relations

State (but do not solve) the recurrence describing the runtime of the following function.

def foo(n):
    if n < 1:
        return 0

    for i in range(n**2):
        print("here")

    foo(n // 2)
Solution

\( T(n) = \begin{cases} \Theta(1), & n = 1\\ T(n/2) + \Theta(n^2)% , & n > 1 \end{cases} \)

Problem #130

Tags: lecture-06, recurrence relations

What is the solution of the below recurrence? State your answer using asymptotic notation as a function of \(n\).

\[ T(n) = \begin{cases} 5 T(n/5) + \Theta(n),& n > 1\\ 1,& n = 1 \end{cases} \]
Solution

\(\Theta(n \log n)\).

At depth \(k\) of the recursion tree there are \(5^k\) subproblems of size \(n/5^k\), so each level does \(5^k \cdot \Theta(n / 5^k) = \Theta(n)\) work. There are \(\log_5 n\) levels, so the total is \(\Theta(n \log n)\).

Problem #131

Tags: lecture-06, recurrence relations

State (but do not solve) the recurrence describing the runtime of the following function.

def foo(n):
    if n <= 1:
        return

    foo(n // 2)

    for j in range(n):
        print(j)

    foo(n // 2)
Solution

\( T(n) = \begin{cases} \Theta(1), & n = 1\\ 2 T(n/2) + \Theta(n), & n > 1 \end{cases} \)

There are two recursive calls, each on an input of size \(n/2\), and the loop between them does \(\Theta(n)\) work.

Problem #132

Tags: lecture-06, recurrence relations

State (but do not solve) the recurrence describing the runtime of the following function.

def foo(n):
    if n < 10:
        print("Hello world.")
        return

    for i in range(n):
        print(i)

    for i in range(6):
        foo(n//3)
Solution

\(T(n) = 6 T(n/3) + \Theta(n)\).

The first loop does \(\Theta(n)\) work, and the second loop makes 6 recursive calls, each on an input of size \(n // 3 \approx n/3\).

Problem #133

Tags: lecture-06, recurrence relations

State (but do not solve) the recurrence describing the runtime of the following function.

def boo(n):
    if n <= 1:
        return

    for i in range(3):
        boo(n // 4)

    for j in range(n):
        print(j)
Solution

\(T(n) = 3 T(n/4) + \Theta(n)\).

Even though the recursive calls are made in a for-loop, the basic idea is the same: there will be three recursive calls, each on an input of size \(n/4\), and thus the recurrence will involve \(3 T(n/4)\). There is \(\Theta(n)\) work done outside the recursive calls, for a full recurrence of \(T(n) = 3 T(n/4) + \Theta(n)\).

Problem #134

Tags: lecture-06, recurrence relations

State (but do not solve) the recurrence relation describing this function's run time.

import random

def foo(n):
    if n <= 2:
        return 1

    for i in range(n):
        for j in range(i, n):
            print(i)

    return foo(n//2) + foo(n//2)
Solution

\(T(n) = 2 T(n/2) + \Theta(n^2)\).

The nested loops are dependent: the inner loop runs \(n - i\) times, for a total of \(n + (n-1) + \ldots + 1 = \Theta(n^2)\). Then there are two recursive calls, each on a problem of size \(n/2\).

Problem #137

Tags: lecture-06, mergesort, recurrence relations

Consider the modification of mergesort shown below, where one of the recursive calls has been replaced by an in-place version of selection_sort. Recall that selection_sort takes \(\Theta(n^2)\) time.

def kinda_mergesort(arr):
    """Sort array in-place."""
    if len(arr) > 1:
        middle = math.floor(len(arr) / 2)
        left = arr[:middle]
        right = arr[middle:]
        mergesort(left)
        selection_sort(right)
        merge(left, right, arr)

What is the time complexity of kinda_mergesort?

Solution

\(\Theta(n^2)\).

The function isn't actually recursive: it calls the regular mergesort on the left half, taking \(\Theta(n \log n)\) time, selection_sort on the right half, taking \(\Theta((n/2)^2) = \Theta(n^2)\) time, and merge, taking \(\Theta(n)\) time. The selection sort dominates, so the total is \(\Theta(n^2)\).

Problem #151

Tags: lecture-05, recurrence relations

Solve the below recurrence, stating the solution in asymptotic notation. Show your work.

\[ T(n) = \begin{cases} T(n/2) + \Theta(n) & n > 1 \\ \Theta(1) & n = 1 \end{cases} \]
Solution

\(\Theta(n)\).

Unrolling several times, we find:

\[ \begin{aligned} T(n) &= T(n/2) + n\\ &= T(n/4) + \frac{n}{2} + n\\ &= T(n/8) + \frac{n}{4} + \frac{n}{2} + n \end{aligned} \]

On the \(k\)th unroll, we'll get:

\[ T(n) = T(n/2^k) + \left[ n + \frac{n}{2} + \frac{n}{4} + \ldots + \frac{n}{2^{k-1}} \right] \]

It will take \(\log_2 n\) unrolls to reach the base case. Substituting this for \(k\), we get:

\[ \begin{aligned} T(n) &= T(n/2^{\log_2 n}) + \left[ n + \frac{n}{2} + \frac{n}{4} + \ldots + \frac{n}{2^{(\log_2 n)-1}} \right] \\ &= T(1) + n\left[ 1 + \frac{1}{2} + \frac{1}{4} + \ldots + \frac{1}{2^{(\log_2 n)-1}} \right] \\ & = T(1) + c n\\ & = \Theta(1) + c n\\ &= \Theta(n) \end{aligned} \]

The sum in brackets is a geometric series, which is at most \(1 + \frac{1}{2} + \frac{1}{4} + \ldots = 2\), so it is a constant \(c\).

Problem #152

Tags: lecture-05, recurrence relations

Solve the below recurrence, stating the solution in asymptotic notation.

\[ T(n) = \begin{cases} T(n/3) + \Theta(n) & n > 1 \\ \Theta(1) & n = 1 \end{cases} \]
Solution

\(\Theta(n)\).

Unrolling several times, we find:

\[ \begin{aligned} T(n) &= T(n/3) + n\\ &= T(n/9) + \frac{n}{3} + n\\ &= T(n/27) + \frac{n}{9} + \frac{n}{3} + n \end{aligned} \]

On the \(k\)th unroll, we'll get:

\[ T(n) = T(n/3^k) + \left[ n + \frac{n}{3} + \frac{n}{9} + \ldots + \frac{n}{3^{k-1}} \right] \]

It will take \(\log_3 n\) unrolls to reach the base case. Substituting this for \(k\), we get:

\[ \begin{aligned} T(n) &= T(n/3^{\log_3 n}) + \Biggl[ n + \frac{n}{3} + \frac{n}{9} + \ldots + \frac{n}{3^{(\log_3 n) - 1}}\Biggr]\\ &= T(1) + n \underbrace{\Biggl[ 1 + \frac{1}{3} + \frac{1}{9} + \ldots + \frac{1}{3^{(\log_3 n) - 1}}\Biggr]}_{\leq 3/2} \end{aligned} \]

Here we've recognized the summation in square brackets as a geometric sum of powers of \(1/3\), which converges to \(3/2\) when there are infinitely many terms. We've seen this sum in solving a couple of homework problems.

\[ \begin{aligned} T(n) &= T(1) + cn\\ &= \Theta(1) + cn\\ &= \Theta(n) \end{aligned} \]

Problem #153

Tags: lecture-06, recurrence relations

Solve the below recurrence, stating the solution in asymptotic notation. Show your work.

\[ T(n) = \begin{cases} 2 T(n/4) + \Theta(n) & n > 1 \\ c & n = 1 \end{cases} \]
Solution

\(\Theta(n)\).

We'll unroll several times:

\[ \begin{aligned} T(n) &= 2 T(n/4) + n \\ &= 4 T(n/16) + \frac{n}{2} + n \\ &= 8 T(n/64) + \frac{n}{4} + \frac{n}{2} + n \\ \end{aligned} \]

The pattern seems to be that on the \(k\)th unroll:

\[ T(n) = 2^k T\left(\frac{n}{4^k} \right)+ n \left( 1 + \frac12 + \frac14 + \ldots + \frac{1}{2^{k-1}} \right) \]

The base case is reached when \(k = \log_4 n\). Plugging this back in, we find:

\[ T(n) = 2^{\log_4 n} T(1) + n \left(1 + \frac12 + \frac 14 + \ldots + \frac{1}{2^{\log_4 n - 1}}\right) \]

If you're unsure of what to do with \(2^{\log_4 n}\), there are a couple of ways forward.

The first (and easiest) way is to realize that \(2^{\log_4 n}\) is smaller than \(2^{\log_2 n} = n\), so \(2^{\log_4 n} = O(n)\). Similarly, we've seen the summation of \(1 + 1/2 + 1/4 + \ldots\) several times—this is a geometric sum, and converges to 2 when there are infinitely many terms. There are a finite number of terms here, so the sum is \(< 2\). Altogether, we have:

\[ T(n) = O(n) T(1) + n \Theta(1) = \Theta(n) \]

The second approach is to remember log properties to simplify \(2^{\log_4 n}\) to \(\sqrt{n}\). This can be seen by using the “change of base” formula:

\[ \log_b x = \frac{\log_a x}{\log_a b}. \]

In this case:

\[ \log_4 n = \frac{\log_2 n}{\log_2 4} = \frac{\log_2 n}{2} \]

And therefore \(2^{\log_4 n} = 2^{(\log_2 n) / 2} = \left(2^{\log_2 n}\right)^{1/2} = n^{1/2} = \sqrt n\). This shows that the first term in the recurrence is \(\Theta(\sqrt n)\). The second term is still \(\Theta(n)\), so the solution is \(\Theta(n)\).

Problem #159

Tags: lecture-06, recurrence relations

What is the time complexity of this function? Here \(n\) is the length of the input array.

import math
def foo(arr):
    if len(arr) == 0:
        return 0

    if len(arr) == 1:
        return arr[0]

    if len(arr) == 2:
        return arr[0] + arr[1]

    one_third = math.floor(len(arr) / 3)
    left = arr[:one_third]
    middle = arr[one_third: one_third * 2]
    right = arr[one_third * 2:]

    for i in range(len(arr)):
        if i < one_third:
            arr[i] = 2 * arr[i] - sum(left)/len(left)
        if i >= one_third and i < one_third * 2:
            arr[i] = 2 * arr[i] - sum(middle)/len(middle)
        if i >= one_third * 2:
            arr[i] = 2 * arr[i] - sum(right)/len(right)

    total = sum(left) + sum(middle) + sum(right)

    return foo(left) + foo(middle) + foo(right) + total
Solution

\(\Theta(n^2)\).

This does \(\Theta(n^2)\) work outside of the recursive calls. The recurrence relation is \(T(n) = 3T(n/3) + \Theta(n^2)\), whose solution is \(\Theta(n^2)\).

Solving this recurrence involves a little bit more than usual, because you have to recognize a geometric sum. When unrolling, you should find that on step \(k\) you get a term of the form \(n^2 + n^2/3 + n^2/3^2 + n^2/3^3 + ... + n^2/3^{k-1}\). This is a geometric sum, which we saw in an earlier homework. If you factor out the \(n^2\), you get something of the form \(1 + 1/3 + 1/3^2 + ...\). There is an exact formula for this, but for our purposes all that matters is that the sum converges to some number (a constant), and so the recurrence is \(\Theta(n^2)\).